/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Jan 3, 2013 3:46:32 PM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package com.kanbox.linux.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

public class FileUtil {

	public static void deleteAll(File file) {
		if (!file.exists()) {
			return;
		}
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File f : files) {
				deleteAll(f);
			}
		}
		file.delete();
	}

	public static int countFiles(File root) {
		if (root.isDirectory()) {
			File[] children = root.listFiles();
			int count = children.length;
			for (File file : children) {
				if (file.isDirectory()) {
					count += countFiles(file);
				}
			}
			return count;
		}
		return 0;
	}

	/**
	 * JDK7 required
	 * 
	 * @param path
	 * @return
	 */
	public static Path getNIOPath(String path) {
		return new File(path).toPath();
	}

	public static int getUID(Path path) throws IOException {
		return (Integer) Files.getAttribute(path, "unix:uid");
	}

	public static int getGID(Path path) throws IOException {
		return (Integer) Files.getAttribute(path, "unix:gid");
	}

	public static int getNLink(Path path) throws IOException {
		return (Integer) Files.getAttribute(path, "unix:nlink");
	}

	public static long getInode(Path path) throws IOException {
		return (Long) Files.getAttribute(path, "unix:ino");
	}

	public static long getRDev(Path path) throws IOException {
		return (Long) Files.getAttribute(path, "unix:rdev");
	}

	public static int getPermission(Path path) throws IOException {
		Set<PosixFilePermission> pers = null;
		pers = Files.getPosixFilePermissions(path);
		int per = 0;
		for (PosixFilePermission posixFilePermission : pers) {
			switch (posixFilePermission) {
			case GROUP_EXECUTE:
				per += 010;
				break;
			case GROUP_READ:
				per += 040;
				break;
			case GROUP_WRITE:
				per += 020;
				break;
			case OTHERS_EXECUTE:
				per += 01;
				break;
			case OTHERS_READ:
				per += 04;
				break;
			case OTHERS_WRITE:
				per += 02;
				break;
			case OWNER_EXECUTE:
				per += 0100;
				break;
			case OWNER_READ:
				per += 0400;
				break;
			case OWNER_WRITE:
				per += 0200;
				break;
			default:
				break;
			}
		}
		return per;
	}

	public static Set<PosixFilePermission> getFilePermissions(int mode) {
		Set<PosixFilePermission> set = new HashSet<>();
		if (((mode >>> 8) & 0x1) == 0x1) {
			set.add(PosixFilePermission.OWNER_READ);
		}
		if (((mode >>> 7) & 0x1) == 0x1) {
			set.add(PosixFilePermission.OWNER_WRITE);
		}
		if (((mode >>> 6) & 0x1) == 0x1) {
			set.add(PosixFilePermission.OWNER_EXECUTE);
		}
		if (((mode >>> 5) & 0x1) == 0x1) {
			set.add(PosixFilePermission.GROUP_READ);
		}
		if (((mode >>> 4) & 0x1) == 0x1) {
			set.add(PosixFilePermission.GROUP_WRITE);
		}
		if (((mode >>> 3) & 0x1) == 0x1) {
			set.add(PosixFilePermission.GROUP_EXECUTE);
		}
		if (((mode >>> 2) & 0x1) == 0x1) {
			set.add(PosixFilePermission.OTHERS_READ);
		}
		if (((mode >>> 1) & 0x1) == 0x1) {
			set.add(PosixFilePermission.OTHERS_WRITE);
		}
		if (((mode) & 0x1) == 0x1) {
			set.add(PosixFilePermission.OTHERS_EXECUTE);
		}
		return set;
	}

	public static void main(String[] args) throws IOException {
		Path p = new File("/home/yezi/123.zip").toPath();
		long uid = (Long) Files.getAttribute(p, "unix:rdev");
		System.out.println(0755);
	}
}
